//
//  ByUIPersonalCenterVC.m
//  itboye_shop_ios
//
//  Created by mc on 17/2/7.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIPersonalCenterVC.h"
// vc
#import "ByUIOrderVC.h"
#import "ByUIAddressVC.h"
#import "ByUIPersonSettingVC.h"             // 个人设置
#import "ByUIMineDataVC.h"
// view
#import "ByViewMineButton.h"
#import "ByViewMineHeaderView.h"
@interface ByUIPersonalCenterVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *table;               // 主table视图
@property (nonatomic, strong) NSArray *titleAry1;
@property (nonatomic, strong) NSArray *titleAry2;
@property (nonatomic, strong) NSArray *imageAry1;
@property (nonatomic, strong) NSArray *imageAry2;
@property (nonatomic, strong) ByViewMineHeaderView *header;
@end

@implementation ByUIPersonalCenterVC
#pragma mark getter
- (UITableView *)table {
    if (!_table) {
        _table = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, SCREEN_WIDTH, SCREEN_HEIGHT  - 49 + 64) style:UITableViewStylePlain];
        _table.delegate = self;
        _table.dataSource = self;
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
        _table.backgroundColor = TABLE_COLOR;
    }
    return _table;
}

-(NSArray *)titleAry1 {
    if (!_titleAry1) {
        _titleAry1 = @[@"待付款",@"待发货",@"待收货",@"待评价",@"退款／售后"];
    }
    return _titleAry1;
}

- (NSArray *)titleAry2 {
    if (!_titleAry2) {
        _titleAry2 = @[@"收藏宝贝",@"关注",@"会员中心",@"收货地址"];
    }
    return _titleAry2;
}

- (NSArray *)imageAry1 {
    if (!_imageAry1) {
        _imageAry1 = @[@"product_detail_tool_shop",@"product_detail_tool_shop",@"product_detail_tool_shop",@"product_detail_tool_shop",@"product_detail_tool_shop"];
    }
    return _imageAry1;
}

- (NSArray *)imageAry2 {
    if (!_imageAry2) {
        _imageAry2 = @[@"product_detail_tool_shop",@"product_detail_tool_shop",@"product_detail_tool_shop",@"product_detail_tool_shop"];
    }
    return _imageAry2;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    
//    [self addBYItemviews:@[@"设置"] mode:ByNavigationItemModeText position:ByNavigationItemPositionModeLeft];
    
   
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat width = [@"设置" getSizeOfString:16].width + 20;
    button.frame = CGRectMake(0, 0, width, 44);
    [button setTitle:@"设置" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = FONT(16);
    [button addTarget:self action:@selector(setVCAction:) forControlEvents:UIControlEventTouchUpInside];
    UIEdgeInsets edge =  UIEdgeInsetsMake(0, 0, 0, 20);
    [button setTitleEdgeInsets:edge];

    [self addCustomItemviews:@[button] position:ByNavigationItemPositionModeLeft];
    [self.view addSubview:self.table];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setNaviAttributeWithTranslucent:YES stateBarLightContent:YES];
    [self createViews];
}


#pragma mark UI
- (void)createViews {
    self.header = [[ByViewMineHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 164)];
    self.header.nameLabel.text = BY_USER.nickname;
    [self.header.iconView setImageWithURL:[NSURL URLWithString:BY_USER.head] placeholderImage:DEFAULT_HEAD];
    __weak ByUIPersonalCenterVC *that = self;
    self.header.tapBlock = ^{
        if (BY_USER) {
            ByUIMineDataVC *vc = [[ByUIMineDataVC alloc] init];
            [that by_normalPush:vc hasNavi:YES];
        }else{
            ByUINLoginVC *vc = [[ByUINLoginVC alloc] init];
            ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
            [that presentViewController:navi animated:YES completion:nil];

        }
        
    };
    self.table.tableHeaderView = self.header;
//    if (BY_USER) {
//        
//    }else{
//        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 164)];
//        view.backgroundColor = BY_THEME_COLOR;
//        self.table.tableHeaderView = view;
//    }
    
}

#pragma mark tableview delegate and dataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"normalCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"normalCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.detailTextLabel.text = @"查看全部订单";
            cell.detailTextLabel.font = FONT(12);
            cell.detailTextLabel.textColor = [UIColor lightGrayColor];
            cell.textLabel.text = @"我的订单";
            cell.textLabel.textColor = [UIColor darkGrayColor];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 43.5, SCREEN_WIDTH, 0.5)];
            line.backgroundColor = BY_LINE_COLOR;
            [cell addSubview:line];
        }else{
            for (NSInteger i = 0; i < self.titleAry1.count; i++) {
                UIImage *image = [UIImage imageNamed:self.imageAry1[i]];
                UIColor *color = [UIColor darkGrayColor];
                ByViewMineButton *button = [[ByViewMineButton alloc] initWithFrame:CGRectMake(i*SCREEN_WIDTH/5, 0, SCREEN_WIDTH/5, 60) Image:image Title:self.titleAry1[i] Font:12 Color:color];
                button.tag = i + 11;
                [button addTarget:self action:@selector(OrderAction:) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:button];
            }
        }
    }else{
        if (indexPath.row == 0) {
            cell.detailTextLabel.text = @"查看更多工具";
            cell.detailTextLabel.font = FONT(12);
            cell.detailTextLabel.textColor = [UIColor lightGrayColor];
            cell.textLabel.text = @"必备工具";
            cell.textLabel.textColor = [UIColor darkGrayColor];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 43.5, SCREEN_WIDTH, 0.5)];
            line.backgroundColor = BY_LINE_COLOR;
            [cell addSubview:line];

        }else{
            for (NSInteger i = 0; i < self.titleAry2.count; i++) {
                UIImage *image = [UIImage imageNamed:self.imageAry2[i]];
                UIColor *color = [UIColor darkGrayColor];
                ByViewMineButton *button = [[ByViewMineButton alloc] initWithFrame:CGRectMake(i*SCREEN_WIDTH/4, 0, SCREEN_WIDTH/4, 100) Image:image Title:self.titleAry2[i] Font:12 Color:color];
                button.tag = i + 20;
                [button addTarget:self action:@selector(OrderAction:) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:button];
            }

        }
    }
    
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
       return 44;
    }else{
        if (indexPath.section == 0) {
            return 60;
        }else{
            return 100;
        }
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [UIView new];
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        if ([self loginIfNoUserId]) {
            ByUIOrderVC *vc = [[ByUIOrderVC alloc] init];
            vc.selectIndex = 0;
            [self by_normalPush:vc hasNavi:YES];
        }
    }
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat yOffset = self.table.contentOffset.y;
    //向上偏移量变正  向下偏移量变负
//    if (yOffset <= -64) {
//        CGFloat h;
//        h = SCREEN_WIDTH/375*204;
//        if (SCREEN_WIDTH < 375) {
//            h = SCREEN_WIDTH/375*204 + 30;
//        }
//        UIImageView *bgImageView = (UIImageView *)[_headerView viewWithTag:8888];
//        CGFloat height = ABS(yOffset) + h - 64;  // 放大的高度
//        CGFloat scale = height/(h);
//        bgImageView.transform = CGAffineTransformMakeScale(1, scale);
//        bgImageView.center = CGPointMake(_headerView.center.x, 0);
//        bgImageView.bottom = 140;
//        [self.viewController.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
//        self.viewController.title = nil;
//    }else if(yOffset < 0 && yOffset > -64){
//        CGFloat apl = (64 - ABS(yOffset))/64;
//        [self.viewController.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:rgba(255, 111, 165, apl)] forBarMetrics:UIBarMetricsDefault];
//        self.viewController.title = nil;
//    }else{
//        [self.viewController.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:MW_THEME_COLOR] forBarMetrics:UIBarMetricsDefault];
//        
//        
//        self.viewController.title = _model.nickname;
//    }
}

#pragma mark 点击事件
- (void)OrderAction:(UIButton *)button {
    if ([self loginIfNoUserId]) {
        if (button.tag >= 11 && button.tag <= 14) {
            ByUIOrderVC *vc = [[ByUIOrderVC alloc] init];
            vc.selectIndex = button.tag - 10;
            [self by_normalPush:vc hasNavi:YES];
        }else if (button.tag == 15){
            ByUIAddressVC *vc = [[ByUIAddressVC alloc] init];
            [self by_normalPush:vc hasNavi:YES];
            
        }
    }
}
//- (void)leftItemsAction:(NSInteger)leftIndex {
//    if ([self loginIfNoUserId]) {
//        ByUIPersonSettingVC *vc = [[ByUIPersonSettingVC alloc] init];
//        [self by_normalPush:vc hasNavi:YES];
//    }
//}

//- (void)rightItemsAction:(NSInteger)rightIndex {
//    
//}

- (void)setVCAction:(UIButton *)button {
    if ([self loginIfNoUserId]) {
        ByUIPersonSettingVC *vc = [[ByUIPersonSettingVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }

}

#pragma mark --------------------


// 如果用户没登录就去登录
- (BOOL)loginIfNoUserId {
    
    if (!BY_USER) {
        ByUINLoginVC *vc = [[ByUINLoginVC alloc] init];
        ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
        [self presentViewController:navi animated:YES completion:nil];
        return NO;
    }
    return YES;
}


@end
